package com.dopies.ecmobile.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.dopies.BeeFramework.activity.BaseActivity;
import com.dopies.BeeFramework.model.BusinessResponse;
import com.dopies.BeeFramework.view.ToastView;
import com.dopies.ecmobile.R;
import com.dopies.ecmobile.adapter.CommentAdapter;
import com.dopies.ecmobile.model.CommentModel;
import com.dopies.ecmobile.model.ProtocolConst;
import com.external.androidquery.callback.AjaxStatus;
import com.external.maxwin.view.XListView;
import com.external.maxwin.view.XListView.IXListViewListener;

public class CommentActivity extends BaseActivity implements IXListViewListener, BusinessResponse {

	private TextView title;
	private ImageView back;
	
	private XListView xlistView;
	private CommentAdapter commentAdapter;
	
	private int goods_id;
	
	private CommentModel commentModel;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.comment);
		
		Intent intent = getIntent();
		goods_id = intent.getIntExtra("id", 0);
		
		title = (TextView) findViewById(R.id.top_view_text);
        Resources resource = getBaseContext().getResources();
        String com=resource.getString(R.string.gooddetail_commit);
		title.setText(com);
		back = (ImageView) findViewById(R.id.top_view_back);
		back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
		xlistView = (XListView) findViewById(R.id.comment_list);
		xlistView.setPullLoadEnable(true);
		xlistView.setRefreshTime();
		xlistView.setXListViewListener(this,1);
		
		commentModel = new CommentModel(this);
		commentModel.addResponseListener(this);
		commentModel.getCommentList(goods_id);
		
	}

	@Override
	public void onRefresh(int id) {
		// TODO Auto-generated method stub
		
		commentModel.getCommentList(goods_id);
	}

	@Override
	public void onLoadMore(int id) {
		// TODO Auto-generated method stub
		commentModel.getCommentsMore(goods_id);
	}
	
	public void setComment() {
		
		if(commentModel.comment_list.size() > 0) {
			xlistView.setVisibility(View.VISIBLE);
			if(commentAdapter == null) {
				commentAdapter = new CommentAdapter(this, commentModel.comment_list);
				xlistView.setAdapter(commentAdapter);
			} else {
				commentAdapter.list = commentModel.comment_list;
				commentAdapter.notifyDataSetChanged();
			}
			
		} else {
			xlistView.setVisibility(View.GONE);
            Resources resource = getBaseContext().getResources();
            String noc=resource.getString(R.string.no_commit);
            ToastView toast = new ToastView(this, noc);
			//Toast toast = Toast.makeText(this, noc, 0);
	        toast.setGravity(Gravity.CENTER, 0, 0);
	        toast.show();
		}
		
	}

	@Override
	public void OnMessageResponse(String url, JSONObject jo, AjaxStatus status)
			throws JSONException {
		// TODO Auto-generated method stub
		if(url.endsWith(ProtocolConst.COMMENTS)) {
			xlistView.setRefreshTime();
			xlistView.stopRefresh();
			xlistView.stopLoadMore();
			if(commentModel.paginated.more == 0) {
				xlistView.setPullLoadEnable(false);
			} else {
				xlistView.setPullLoadEnable(true);
			}
			setComment();
		}
		
	}
	
}
